import httpx
from syncora_sdk.client import SyncoraClient
from pydantic import HttpUrl
from pathlib import Path
from syncora_sdk.schemas.dto.Base import BaseDto


class S3Uploder:
    def __init__(self, client: SyncoraClient):
        self.client = client

    def get_presigned_url(self, fileName: str) -> dict | None:
        response = self.client.post(
            "/ai/get-presigned-url",
            json={"filename": fileName, "contentType": "text/csv"},
        )
        data = BaseDto(response).data
        return data

    def upload_file(self, upload_url: HttpUrl, file_path: Path):
        file = Path(file_path)
        if not file.exists():
            raise FileNotFoundError(f"File not found : {file_path}")
        with open(file, "rb") as f:
            response = httpx.put(str(upload_url), content=f.read())
            response.raise_for_status()
            return True
